package com.example.kafka.controller;

import com.example.kafka.domain.CabinetDoor;
import com.example.kafka.domain.ConveyerBelt;
import com.example.kafka.domain.Screen;
import com.example.kafka.mapper.*;
import com.example.kafka.vo.Result;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@RestController
@RequestMapping("/test")
public class TestController {


    @Autowired
    private CabinetDoorMapper cabinetDoorMapper;

    @Autowired
    private ConveyerBeltMapper conveyerBeltMapper;

    @Autowired
    private MoneyBeltMapper moneyBeltMapper;

    @Autowired
    private PrinteMachineMapper printeMachineMapper;

    @Autowired
    private ScreenMapper screenMapper;


    @GetMapping("/queryAllMachineStatus")
    public Result queryAllMachineStatus() {
        /*
        逻辑：
        1. 获取所有的machineId
        2. 遍历获取每个id的所有部件状态
        3. 封装为map返回
         */

        // [1] 获取所有的machineId
        List<Integer> ids = cabinetDoorMapper.queryAllByMachineIdIntegers();

        // [2] 遍历获取每个id的所有部件状态
        List<Map<String, Object>> resultMap = new ArrayList<>();
        for (Integer id : ids) {
            Map<String, Object> statusMap = getMachineStatusById(id);
            resultMap.add(statusMap);
        }
        return Result.success(resultMap, resultMap.size());
    }


    /**
     * 根据machineId获取所有部件状态的通用方法
     * @param machineId 机器id
     * @return 当前机器的所有部件状态
     */
    public Map<String, Object> getMachineStatusById(Integer machineId) {
        Map<String, Object> statusMap = new LinkedHashMap<>();


        /**
         * 柜门
         */
        // 1. 柜门
        // 使用mapper从存数表查询最近一次取数的数据
        CabinetDoor cabinetDoor = cabinetDoorMapper.queryLatestData(machineId);
        statusMap.put("machineId",cabinetDoor.getMachineId());
        statusMap.put("machineName",cabinetDoor.getMachineName());
        // 获取电压属性
        Integer doorVoltage = cabinetDoor.getVoltage();
        // 获取电流属性
        Integer doorCurrent = cabinetDoor.getCurrent();


        boolean doorStatus = false;
        if (doorVoltage == 0 && doorCurrent >= 1 && doorCurrent <= 3) {
            // 1. 电压为 0  电流1-3 异常
            doorStatus = false;
        } else if (doorVoltage == 0 && doorCurrent >= 20 && doorCurrent <= 25) {
            // 2. 电压为 0  电流 20 -25 异常
            doorStatus = false;
        } else if (doorVoltage >= 0 && doorVoltage <= 1 && doorCurrent == 0) {
            // 3. 电流 0  电压 0-1 异常
            doorStatus = false;
        } else if (doorVoltage >= 8 && doorVoltage <= 10 && doorCurrent == 0) {
            // 4. 电流 0  电压 8-10
            doorStatus = false;
        } else if (doorVoltage >= 1 && doorVoltage <= 5 && doorCurrent >= 4 && doorCurrent <= 20) {
            // 5. 电流 4-20  &&   电压 1-5
            doorStatus = true;
        }
        statusMap.put("柜门", doorStatus);


        /**
         * 钞带
         */
        // todo:获取钞带....
        // 传送带
        ConveyerBelt conveyerBelt = conveyerBeltMapper.queryLatestData(machineId);
        // 电压
        Integer conveyerBeltVoltage = conveyerBelt.getVoltage();
        // 电压
        Integer conveyerBeltRate = conveyerBelt.getRate();

        // 传送带状态
        boolean conveyerBeltStatus = false;

        // todo:根据规则判断状态是 true 或者 false


        /**
         * 显示屏
         */

        Screen screen = screenMapper.queryLatestData(machineId);
        Integer screenCurrent = screen.getCurrent();
        Integer screenVoltage = screen.getVoltage();

        boolean screenStatus = false;
        if (screenVoltage == 0 && screenCurrent >= 1 && screenCurrent <= 3) {
            // 1. 电压为 0  电流1-3 异常
            screenStatus = false;
        } else if (screenVoltage == 0 && screenCurrent >= 20 && screenCurrent <= 25) {
            // 2. 电压为 0  电流 20 -25 异常
            screenStatus = false;
        } else if (screenVoltage >= 0 && screenVoltage <= 1 && screenCurrent == 0) {
            // 3. 电流 0  电压 0-1 异常
            screenStatus = false;
        } else if (screenVoltage >= 8 && screenVoltage <= 10 && screenCurrent == 0) {
            // 4. 电流 0  电压 8-10
            screenStatus = false;
        } else if (screenVoltage >= 1 && screenVoltage <= 5 && screenCurrent >= 4 && screenCurrent <= 20) {
            // 5. 电流 4-20  &&   电压 1-5
            screenStatus = true;
        }
        statusMap.put("显示屏", screenStatus);

        // 封装最终结果集

        return statusMap;
    }


}
